const autoprefixer = require('gulp-autoprefixer'),
      babel        = require('gulp-babel'),
      browserify   = require('gulp-browserify'),
      concat       = require('gulp-concat'),
      connect      = require('gulp-connect'),
      del          = require('del'),
      gulp         = require('gulp'),
      less         = require('gulp-less'),
      minifyCSS    = require('gulp-minify-css'),
      rename       = require('gulp-rename'),
      uglify       = require('gulp-uglify'),
      sequence     = require('gulp-sequence'),
      open         = require('gulp-open'),
      fileInclude  = require('gulp-file-include'),
      replace      = require('gulp-replace'),
      stripDebug   = require('gulp-strip-debug')

/* clean */
gulp.task('clean', () => {
  return (
    del('./dist/**/*')
  )
})

/* html */
gulp.task('html', () => {
  return (
    gulp.src([
          './src/html/*.html'])
        .pipe(fileInclude({
          prefix: '@@',
          basepath: './src/html/public/'
        }))
        .pipe(gulp.dest('./dist/'))
        .pipe(connect.reload())
  )
})

/* jsVendor */
gulp.task('jsVendor', () => {
  return (
    gulp.src([
          './src/js/vendor/jquery-1.9.1.min.js',
          './src/js/vendor/moment.min.js',
          './src/js/vendor/jquery.validate.min.js',
          './src/js/vendor/bootstrap.min.js',
          './src/js/vendor/swiper.min.js'
        ])
        .pipe(concat('vendor.js'))
        .pipe(gulp.dest('./dist/js/'))
        .pipe(connect.reload())
  )
})


/* js */
gulp.task('js', () => {
  return (
    gulp.src([
          './src/js/*.js'
        ])
        .pipe(babel())
        .pipe(browserify({
          transform: ['babelify']
        }))
        // .pipe(stripDebug())
        .pipe(uglify())
        .pipe(gulp.dest('./dist/js'))
        .pipe(connect.reload())
  )
})

/* less */
gulp.task('less', () => {
  return (
    gulp.src([
        './src/less/bootstrap.less',
        ])
        .pipe(less())
        .pipe(autoprefixer({
          browsers: ['> 0.01%']
         }))
        .pipe(minifyCSS())
        .pipe(rename('index.css'))
        .pipe(gulp.dest('./dist/css'))
        .pipe(connect.reload())
  )
})

/* css */
gulp.task('css', () => {
  return (
    gulp.src([
          './src/less/vendor/animate.css',
          './src/less/vendor/swiper.min.css'
        ])
        .pipe(concat('vendor.css'))
        .pipe(gulp.dest('./dist/css/'))
  )
})

/* favicon */
gulp.task('favicon', () => {
  return (
    gulp.src('src/favicon.ico')
        .pipe(gulp.dest('./dist/'))
  )
})

/* font */
gulp.task('font', () => {
  return (
    gulp.src('./src/font/**')
        .pipe(gulp.dest('./dist/font'))
  )
})

/* img */
gulp.task('img', () => {
  return (
    gulp.src('./src/img/**')
        .pipe(gulp.dest('./dist/img'))
  )
})

/* server */
gulp.task('connect', () => {
  connect.server({
    root: 'dist',
    livereload: true
  })
})

/* open browser */
gulp.task('open', () => {
  gulp.src(__filename)
      .pipe(open({
        uri: 'http://localhost:8080',
        app: 'chrome'
      }))
})

/* watch */
gulp.task('watch', () => {
  
  gulp.watch(['src/**/*.html'], ['html'])
  gulp.watch('src/less/**/*.less', ['less'])
  gulp.watch(['src/css/**/*.css'], ['css'])
  gulp.watch(['src/img/**/*'], ['img'])
  gulp.watch(['src/js/vendor/**/*.js'], ['jsVendor'])

  return gulp.watch(['src/js/*.js'], (e) => {

    gulp.src(e.path)
        .pipe(babel())
        .pipe(browserify({
          transform: ['babelify']
        }))
        // .pipe(stripDebug())
        .pipe(uglify())
        .pipe(gulp.dest('./dist/js'))
        .pipe(connect.reload())
    
  })

})

/* default */
gulp.task('default', ['clean'], () => {
  sequence('html','favicon','jsVendor', 'js', 'css', 'less', 'font', 'img', 'connect', 'open', 'watch')()
})